package org.tnaggs.project.validate;

import org.springframework.validation.Errors;
import org.springframework.validation.Validator;
import org.tnaggs.project.model.Game;

public class GameValidator implements Validator {

	@Override
	public boolean supports(Class<?> arg0) {
		return Game.class.equals(arg0);
	}

	@Override
	public void validate(Object arg0, Errors errors) {
		Game game = (Game)arg0;

		if(game == null) {
			errors.rejectValue("homeTeamID", "game.team.none", "Both teams must be filled in");
		} else if (game.getHomeTeamID() == game.getAwayTeamID()){
			errors.rejectValue("awayTeamID", "game.team.same", "Teams cannot play themselves");
		} else if (game.getAwayTeamID() == -1) {
			errors.rejectValue("awayTeamID", "game.team.none", "Away Team must be selected");
		}  else if (game.getHomeTeamID() == -1) {
			errors.rejectValue("homeTeamID", "game.team.none", "Home Team must be selected");
		}  else if (game.getLocationID() == -1) {
			errors.rejectValue("locationID", "game.local.none", "Location must be selected");
		}

	}

}
